package grapid.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * 邮件发送类
 * 
 * @author grapid
 * @version 1.0
 * 
 */
public class GJavaMail {

	/**
	 * 主机地址
	 */
	private static String host = "";
	/**
	 * 用户名
	 */
	private static String uname = "";
	/**
	 * 密码
	 */
	private static String password = "";

	/**
	 * 发送人邮箱
	 */
	private static String from = "";
	/**
	 * 发送人名称
	 */
	private static String fromName = "";

	/**
	 * 构造函数
	 * 
	 * @param _host
	 *            地址
	 * @param _uname
	 *            用户名
	 * @param _password
	 *            密码
	 */
	public GJavaMail(String _host, String _uname, String _password) {
		host = _host;
		uname = _uname;
		password = _password;
	}

	/**
	 * 构造函数
	 * 
	 * @param _host
	 *            地址
	 * @param _uname
	 *            用户名
	 * @param _password
	 *            密码
	 * @param _from
	 *            来源Email
	 * @param _fromName
	 *            来自
	 */
	public GJavaMail(String _host, String _uname, String _password, String _from, String _fromName) {
		host = _host;
		uname = _uname;
		password = _password;
		from = _from;
		fromName = _fromName;
	}

	/**
	 * 邮件发送类
	 * 
	 * @param to
	 *            发送到
	 * @param subject
	 *            主题
	 * @param content
	 *            内容
	 * @param file
	 *            附件
	 * @param date
	 *            时间
	 * @param from
	 *            来源Email
	 * @param fromName
	 *            来自
	 * @throws MessagingException
	 */
	public static void send(String to, String subject, String content, File file, Date date, String from,
			String fromName) throws MessagingException {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", host);
		props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", "465");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.quitwait", "false");
		Session session = Session.getDefaultInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(uname, password);
			}
		});
		Message msg = new MimeMessage(session);
		MimeMessageHelper messageHelp = new MimeMessageHelper((MimeMessage) msg, file == null ? false : true, "GBK");
		try {
			messageHelp.setFrom(from, fromName);
		} catch (UnsupportedEncodingException e) {
			messageHelp.setFrom(from);
		}
		messageHelp.setTo(to);
		messageHelp.setSubject(subject);
		messageHelp.setText(content, true);
		if (file != null) {
			try {
				messageHelp.addAttachment(MimeUtility.encodeWord(file.getName()), file);
			} catch (UnsupportedEncodingException e) {
			}
		}
		// msg.setFrom(new InternetAddress(JavaMail.from));
		// msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,
		// false));
		// msg.setSubject(subject);
		// msg.setText(content);
		if (date != null)
			msg.setSentDate(date);
		Transport.send(msg);
	}

	/**
	 * 邮件发送类
	 * 
	 * @param to
	 *            发送到
	 * @param subject
	 *            主题
	 * @param content
	 *            内容
	 * @param file
	 *            附件
	 * @param date
	 *            时间
	 * @throws MessagingException
	 */
	public static void send(String to, String subject, String content, File file, Date date) throws MessagingException {
		send(to, subject, content, file, date, from, fromName);
	}

	/**
	 * 获取主机地址
	 * 
	 * @return 返回主机地址
	 */
	public static String getHost() {
		return host;
	}

	/**
	 * 设置主机地址
	 * 
	 * @param host
	 *            主机地址
	 */
	public static void setHost(String host) {
		GJavaMail.host = host;
	}

	/**
	 * 获取用户名
	 * 
	 * @return 返回用户名
	 */
	public static String getUname() {
		return uname;
	}

	/**
	 * 设置用户名
	 * 
	 * @param uname
	 *            用户名
	 */
	public static void setUname(String uname) {
		GJavaMail.uname = uname;
	}

	/**
	 * 获取密码
	 * 
	 * @return 返回密码
	 */
	public static String getPassword() {
		return password;
	}

	/**
	 * 设置密码
	 * 
	 * @param password
	 *            密码
	 */
	public static void setPassword(String password) {
		GJavaMail.password = password;
	}

	/**
	 * 获取发送人邮箱地址
	 * 
	 * @return 返回发送人邮箱地址
	 */
	public static String getFrom() {
		return from;
	}

	/**
	 * 设置发送人邮箱地址
	 * 
	 * @param from
	 *            发送人邮箱地址
	 */
	public static void setFrom(String from) {
		GJavaMail.from = from;
	}

	/**
	 * 获取发送人姓名
	 * 
	 * @return 返回
	 */
	public static String getFromName() {
		return fromName;
	}

	/**
	 * 设置发送人姓名
	 * 
	 * @param fromName
	 *            发送人姓名
	 */
	public static void setFromName(String fromName) {
		GJavaMail.fromName = fromName;
	}
}
